package com.rabbit.demo.direct;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @ClassName AutoRouteService
 * @Description TODO
 * @Author ylr
 * @Date 2021/7/19 13:22
 * @Version 1.0
 */

@Component
@Slf4j
@CacheConfig(cacheNames = {"HelloWorldCache"})
public class AutoRouteService {
    @Resource
    CacheManager cacheManager;


    public  void route(MyMessgae mqMsg) {
        String msg = mqMsg.toString();
        log.info("接收消息开始处理: {}", msg);
        log.info("放入缓存");
        String id = mqMsg.getId();
        Cache cache = cacheManager.getCache("HelloWorldCache");
        cache.put(id,msg);

    }

    public String getMsg(String id){
        log.info("控制器获取消息 [{}] 开始处理:", id);
        Cache cache = cacheManager.getCache("HelloWorldCache");
        assert cache != null;
        return cache.get(id,String.class);
    }

//
//
//    @CachePut(value = "HelloWorldCache",key = "#id")
//    public String save(String id) {
//        log.info("id为 " + id + " 的数据做了缓存");
//        return id;
//    }
//
//    /**
//     * @param id
//     * @CacheEvict从缓存people中删除key为id的数据
//     */
//    @CacheEvict(value = "HelloWorldCache",key = "#msg")
//    public void remove(String id) {
//        log.info("删除了id为" + id + "的数据缓存");
//    }
//
//    /**
//     * @param id
//     * @return
//     * @Cacheable缓存key为person的id数据到缓存people中
//     */
//    @Cacheable(value = "HelloWorldCache",key = "#id")
//    public String findOne(String id) {
//        log.info("为msg：" + id + "数据做了缓存");
//        return id;
//    }
}
